package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.WordStockDO;
import com.xgimi.open.skill.datamanager.domain.mapper.WordStockMapper;
import com.xgimi.open.skill.datamanager.domain.service.WordStockService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【word_stock(词库表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:23:48
 */
@Service
public class WordStockServiceImpl extends ServiceImpl<WordStockMapper, WordStockDO>
        implements WordStockService {

    @Override
    public List<WordStockDO> queryWordStockByNameAndVid(WordStockDO wordStockDO) {
        LambdaQueryWrapper<WordStockDO> wrapper = new LambdaQueryWrapper();
        wrapper.eq(!StringUtils.isEmpty(wordStockDO.getWordName()), WordStockDO::getWordName, wordStockDO.getWordName());
        wrapper.eq(!Objects.isNull(wordStockDO.getSkillVid()), WordStockDO::getSkillVid, wordStockDO.getSkillVid());
        return this.list(wrapper);
    }

    @Override
    public Boolean saveWordStock(WordStockDO wordStockDO) {
        return this.save(wordStockDO);
    }

    @Override
    public List<WordStockDO> listBySkillVid(Long skillVid) {
        if (Objects.isNull(skillVid)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<WordStockDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(skillVid), WordStockDO::getSkillVid, skillVid);
        return this.list(wrapper);
    }

    @Override
    public WordStockDO queryById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<WordStockDO> queryByNameByVidAndNoId(WordStockDO wordStockDO) {
        LambdaQueryWrapper<WordStockDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WordStockDO::getWordName, wordStockDO.getWordName());
        queryWrapper.eq(WordStockDO::getSkillVid, wordStockDO.getSkillVid());
        queryWrapper.ne(WordStockDO::getId, wordStockDO.getId());
        return this.list(queryWrapper);
    }

    @Override
    public Boolean deleteBySkillVid(Long skillVid) {
        LambdaQueryWrapper<WordStockDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WordStockDO::getSkillVid, skillVid);
        return this.remove(queryWrapper);
    }

    @Override
    public IPage<WordStockDO> pageQuery(IPage<WordStockDO> page, WordStockDO mapperReq) {
        LambdaQueryWrapper<WordStockDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getSkillVid()), WordStockDO::getSkillVid, mapperReq.getSkillVid());
        queryWrapper.orderByDesc(WordStockDO::getCreatedAt);
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean updateByIdAndVersion(WordStockDO wordStockDO) {
        LambdaUpdateWrapper<WordStockDO> updateWrapper = new LambdaUpdateWrapper<>();
        Integer updateVersion = wordStockDO.getUpdateVersion();
        updateWrapper.eq(WordStockDO::getUpdateVersion, updateVersion);
        updateWrapper.eq(WordStockDO::getId, wordStockDO.getId());
        // 版本自增
        wordStockDO.setUpdateVersion(updateVersion + 1);
        return this.update(wordStockDO, updateWrapper);
    }

    @Override
    public boolean removeBySkillVids(List<Long> skillVids) {
        if (CollectionUtils.isEmpty(skillVids)) {
            return true;
        }
        LambdaQueryWrapper<WordStockDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(WordStockDO::getSkillVid, skillVids);
        return this.remove(queryWrapper);
    }

    @Override
    public Boolean physicalDeleteBySkillVid(Long skillVid) {
        return this.baseMapper.physicalDeleteBySkillVid(skillVid);
    }
}




